ExposedMembers.AUS = ExposedMembers.AUS or {};
ExposedMembers.AUS.Utils = ExposedMembers.AUS.Utils or {};
AUSUtils = ExposedMembers.AUS.Utils;

local function CivilizationHasTrait(playerId, sTrait)
	if playerId == nil then
		print("ERROR playerId为nil")
		return false;
	end
	-- 根据 property 判断
	local player = Players[playerId];
	if not player then
		print("ERROR 该文明不存在")
		return false;
	end
	if player:GetProperty(sTrait .. "_CAPTURED") == true then
		return true;
	end
	
	-- 查表判断
	local playerConfig = PlayerConfigurations[playerId]
	local sCiv = playerConfig:GetCivilizationTypeName()
	for tRow in GameInfo.CivilizationTraits() do
		if (tRow.CivilizationType == sCiv and tRow.TraitType == sTrait) then
			return true;
		end
	end

	return false;
end
AUSUtils.CivilizationHasTrait = CivilizationHasTrait;

local function LeaderHasTrait(playerId, sTrait)
	if playerId == nil then
		print("ERROR playerId为nil")
		return false;
	end
	-- 根据 property 判断
	local player = Players[playerId];
	if not player then
		print("ERROR 该文明不存在")
		return false;
	end
	if player:GetProperty(sTrait .. "_CAPTURED") == true then
		return true;
	end

	-- 查表判断
	local playerConfig = PlayerConfigurations[playerId]
	local sLeader = playerConfig:GetLeaderTypeName()
	for tRow in GameInfo.LeaderTraits() do
		if (tRow.LeaderType == sLeader and tRow.TraitType == sTrait) then return
			true;
		end
	end

	return false;
end
AUSUtils.LeaderHasTrait = LeaderHasTrait;

local function GetDistrictIndex(districtType)
	local row = GameInfo.Districts[districtType]
	if row then
		return row.Index
	end
	return nil
end
AUSUtils.GetDistrictIndex = GetDistrictIndex;

local function IsDistrictType(districtType, targetType, noUniqueReplace)
	local index = AUSUtils.GetDistrictIndex(targetType)
	if index then
		if districtType == index then
			return true
		end
		if noUniqueReplace then
			return false
		end
		for tRow in GameInfo.DistrictReplaces() do
			if tRow.ReplacesDistrictType == targetType then
				if districtType == AUSUtils.GetDistrictIndex(tRow.CivUniqueDistrictType) then
					return true
				end
			end
		end
	end
	return false
end
AUSUtils.IsDistrictType = IsDistrictType;

local function CityHasDistrict(city, DistrictType)
	local district_index = AUSUtils.GetDistrictIndex(DistrictType)
	if city:GetDistricts():HasDistrict(district_index) and AUSUtils.IsDistrictComplete(city, district_index) then return true; end
	
	for row in GameInfo.DistrictReplaces() do
		if row.ReplacesDistrictType == DistrictType then
			district_index = AUSUtils.GetDistrictIndex(row.CivUniqueDistrictType)
			if city:GetDistricts():HasDistrict(district_index) and AUSUtils.IsDistrictComplete(city, district_index) then
				return true
			end
		end
	end
end
AUSUtils.CityHasDistrict = CityHasDistrict;

-- 判断区域是否完成
local function IsDistrictComplete(city, districtId)
	if city == nil then return false; end

	local district = city:GetDistricts():GetDistrict(districtId)

	if district == nil then return false; end

	if district:IsPillaged() then return false; end

	return district:IsComplete()
end
AUSUtils.IsDistrictComplete = IsDistrictComplete